#include "highscores.h"
#include "ui_highscores.h"


Highscores::Highscores(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Highscores)
{
    ui->setupUi(this);
    initScores1();
    initScores2();
    initScores3();
}

Highscores::~Highscores()
{
    delete ui;
    for (Score* s : scores_1) { delete s; }
    for (Score* s : scores_2) { delete s; }
    for (Score* s : scores_3) { delete s; }
}

void Highscores::on_btnClose_clicked()
{
    emit updateWindowState(true, false, false, false);

}

void Highscores::closeEvent(QCloseEvent* event)
{
    (void)event;
    on_btnClose_clicked();
}

void Highscores::addHighscores(QString name, int score, int duration)
{
    if (duration == 1) { addHighscore1(name, score); }
    else if (duration == 2) { addHighscore2(name, score); }
    else if (duration == 3) { addHighscore3(name, score); }
    showAllTheScores();
}

void Highscores::showAllTheScores()
{
    showScores1();
    showScores2();
    showScores3();
}

// Methods for scores_1
void Highscores::initScores1()
{
    scores_1.push_back(new Score(this->ui->lblName1_1->text(), this->ui->lblScore1_1->text().toInt()));
    scores_1.push_back(new Score(this->ui->lblName2_1->text(), this->ui->lblScore2_1->text().toInt()));
    scores_1.push_back(new Score(this->ui->lblName3_1->text(), this->ui->lblScore3_1->text().toInt()));
    scores_1.push_back(new Score(this->ui->lblName4_1->text(), this->ui->lblScore4_1->text().toInt()));
    scores_1.push_back(new Score(this->ui->lblName5_1->text(), this->ui->lblScore5_1->text().toInt()));
}

void Highscores::addHighscore1(QString name, int score)
{
    Score* newScore = new Score(name, score);
    for (unsigned int i = 0; i < scores_1.size(); ++i)
    {
        if (newScore->getScore() > scores_1.at(i)->getScore())
        {
            scores_1.insert(scores_1.begin()+i, newScore);
            scores_1.pop_back();
            return;
        }
    }
}

void Highscores::showScores1()
{
    this->ui->lblName1_1->setText(scores_1.at(0)->getUsername());
    this->ui->lblName2_1->setText(scores_1.at(1)->getUsername());
    this->ui->lblName3_1->setText(scores_1.at(2)->getUsername());
    this->ui->lblName4_1->setText(scores_1.at(3)->getUsername());
    this->ui->lblName5_1->setText(scores_1.at(4)->getUsername());
    this->ui->lblScore1_1->setText(QString::number(scores_1.at(0)->getScore()));
    this->ui->lblScore2_1->setText(QString::number(scores_1.at(1)->getScore()));
    this->ui->lblScore3_1->setText(QString::number(scores_1.at(2)->getScore()));
    this->ui->lblScore4_1->setText(QString::number(scores_1.at(3)->getScore()));
    this->ui->lblScore5_1->setText(QString::number(scores_1.at(4)->getScore()));
}

// Methods for scores_2
void Highscores::initScores2()
{
    scores_2.push_back(new Score(this->ui->lblName1_2->text(), this->ui->lblScore1_2->text().toInt()));
    scores_2.push_back(new Score(this->ui->lblName2_2->text(), this->ui->lblScore2_2->text().toInt()));
    scores_2.push_back(new Score(this->ui->lblName3_2->text(), this->ui->lblScore3_2->text().toInt()));
    scores_2.push_back(new Score(this->ui->lblName4_2->text(), this->ui->lblScore4_2->text().toInt()));
    scores_2.push_back(new Score(this->ui->lblName5_2->text(), this->ui->lblScore5_2->text().toInt()));
}

void Highscores::addHighscore2(QString name, int score)
{
    Score* newScore = new Score(name, score);
    for (unsigned int i = 0; i < scores_2.size(); ++i)
    {
        if (newScore->getScore() > scores_2.at(i)->getScore())
        {
            scores_2.insert(scores_2.begin()+i, newScore);
            scores_2.pop_back();
            return;
        }
    }
}

void Highscores::showScores2()
{
    this->ui->lblName1_2->setText(scores_2.at(0)->getUsername());
    this->ui->lblName2_2->setText(scores_2.at(1)->getUsername());
    this->ui->lblName3_2->setText(scores_2.at(2)->getUsername());
    this->ui->lblName4_2->setText(scores_2.at(3)->getUsername());
    this->ui->lblName5_2->setText(scores_2.at(4)->getUsername());
    this->ui->lblScore1_2->setText(QString::number(scores_2.at(0)->getScore()));
    this->ui->lblScore2_2->setText(QString::number(scores_2.at(1)->getScore()));
    this->ui->lblScore3_2->setText(QString::number(scores_2.at(2)->getScore()));
    this->ui->lblScore4_2->setText(QString::number(scores_2.at(3)->getScore()));
    this->ui->lblScore5_2->setText(QString::number(scores_2.at(4)->getScore()));
}

//Methods for scores_3
void Highscores::initScores3()
{
    scores_3.push_back(new Score(this->ui->lblName1_3->text(), this->ui->lblScore1_3->text().toInt()));
    scores_3.push_back(new Score(this->ui->lblName2_3->text(), this->ui->lblScore2_3->text().toInt()));
    scores_3.push_back(new Score(this->ui->lblName3_3->text(), this->ui->lblScore3_3->text().toInt()));
    scores_3.push_back(new Score(this->ui->lblName4_3->text(), this->ui->lblScore4_3->text().toInt()));
    scores_3.push_back(new Score(this->ui->lblName5_3->text(), this->ui->lblScore5_3->text().toInt()));
}

void Highscores::addHighscore3(QString name, int score)
{
    Score* newScore = new Score(name, score);
    for (unsigned int i = 0; i < scores_3.size(); ++i)
    {
        if (newScore->getScore() > scores_3.at(i)->getScore())
        {
            scores_3.insert(scores_3.begin()+i, newScore);
            scores_3.pop_back();
            return;
        }
    }
}

void Highscores::showScores3()
{
    this->ui->lblName1_3->setText(scores_3.at(0)->getUsername());
    this->ui->lblName2_3->setText(scores_3.at(1)->getUsername());
    this->ui->lblName3_3->setText(scores_3.at(2)->getUsername());
    this->ui->lblName4_3->setText(scores_3.at(3)->getUsername());
    this->ui->lblName5_3->setText(scores_3.at(4)->getUsername());
    this->ui->lblScore1_3->setText(QString::number(scores_3.at(0)->getScore()));
    this->ui->lblScore2_3->setText(QString::number(scores_3.at(1)->getScore()));
    this->ui->lblScore3_3->setText(QString::number(scores_3.at(2)->getScore()));
    this->ui->lblScore4_3->setText(QString::number(scores_3.at(3)->getScore()));
    this->ui->lblScore5_3->setText(QString::number(scores_3.at(4)->getScore()));
}
